package com.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.common.R;
import com.erp.entity.Goods;
import com.erp.entity.Order;
import com.erp.service.OrderService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
@RequiresRoles(value = {"SupperAdministrator","LogisticsAdministrator"} ,logical = Logical.OR )
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping
    public R<String> save(@RequestBody Order order) {
        boolean save = orderService.save(order);
        if (!save) {
            return R.error("error");
        }
        return R.success("订单新增成功");
    }

    @DeleteMapping
    public R<String> delete(Long id) {
        boolean b = orderService.removeById(id);
        if (!b) {
            return R.error("error");
        }
        return R.success("订单删除成功");
    }

    @GetMapping("/{id}")
    public R<Order> find(@PathVariable Long id) {
        Order byId = orderService.getById(id);
        return R.success(byId);
    }

    @GetMapping("/page")
    public R<Page<Order>> findpage(int page, int pageSize,String name) {
        LambdaQueryWrapper<Order> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(Order::getCreateDateTime);
        if (name!=null){
            lambdaQueryWrapper.like(Order::getGoods,name);
        }
        Page<Order> orderPage = new Page<>(page,pageSize);
        orderService.page(orderPage,lambdaQueryWrapper);
        return R.success(orderPage);
    }


    @PutMapping
    public R<String> update(@RequestBody Order order){

        boolean b = orderService.updateById(order);
        if (!b) {
            return R.error("error");
        }
        return R.success("订单更改成功");
    }


}
